package com.escool.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.SgUsuarios;
import com.escool.model.SgUsuariosHasSgRoles;
import com.escool.model.TbPersonal;
import com.escool.model.TbResponsable;
import com.escool.service.EmpleadoService;
import com.escool.service.PagadorService;
import com.escool.service.SgUsuarioService;



@Controller
public class EmpleadoController {
	
	@Autowired
	EmpleadoService empleadoService;
	
	@Autowired
	SgUsuarioService userService;
	
	@RequestMapping("showEmpleado")
	public String listNiveles(Map<String, Object> map){
		map.put("tbPersonal", new TbPersonal());
		map.put("empleadoList",empleadoService.listEmpleado());
		return "/ingresoEmpleado";
	}
	
	@RequestMapping(value = "addEmpleado", method = RequestMethod.POST)
	public String addNiveles(@ModelAttribute("tbPersonal") @Valid TbPersonal tbPersonal, BindingResult result){
		if (!result.hasErrors()) {
			//estudiantesService.addAlumnos(tbalumnos);
			try{
				empleadoService.addEmpleado(tbPersonal);
				//Crear usuario
				SgUsuarios user = new SgUsuarios();
				user.setNombreUsuario(tbPersonal.getCodigoEmp());
				user.setPassword(tbPersonal.getCodigoEmp());
				user.setNumeroEmpleado(tbPersonal.getCodigoEmp());
				userService.addUsuarios(user);
				//Enrolar usuario alumno
				SgUsuariosHasSgRoles role = new SgUsuariosHasSgRoles();
				role.setSgUsuarios(user);
				role.setSgRoles(userService.findRole(3));
				userService.addUsarioRol(role);
				System.out.println("Empelado " + role.getSgUsuarios().getNombreUsuario() + "con rol " + role.getSgRoles().getNombreRol());
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showEmpleado";
		}
		return "";
	}
	
	@RequestMapping(value = "editEmpleado", method = RequestMethod.POST)
	public String editEmpleado(@ModelAttribute("tbPersonal") @Valid TbPersonal tbPersonal, BindingResult result){
		if (!result.hasErrors()) {
			try{
				empleadoService.updateEmplado(tbPersonal);
				return "redirect:/showEmpleado";
		}catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
		return "";
	}
	
	@RequestMapping(value = "delEmpleado", method = RequestMethod.POST)
	public String delEmpleado(@ModelAttribute("tbPersonal") @Valid TbPersonal tbPersonal, BindingResult result){
		if (!result.hasErrors()) {
			try{
				empleadoService.delEmpleado(tbPersonal.getIdPersonal());
				return "redirect:/showEmpleado";
		}catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
		return "";
	}
	
	
}
